package at.jku.isse.ecco.web.rest.resource;

import java.io.IOException;
import java.net.URI;
import java.util.Iterator;

import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;


@Singleton
@Path("service")
public class MyResource {

    @Path("{sub_path:[a-zA-Z0-9]*}")
    @GET
    @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
    @Produces(MediaType.TEXT_PLAIN)
    public String getResourceName(
            @PathParam("sub_path") String resourceName,
            @DefaultValue("Just a test!") @QueryParam("desc") String description,
            @Context Request request,
            @Context UriInfo uriInfo,
            @Context HttpHeaders httpHeader) {
        System.out.println(this.hashCode());

//		将HTTP请求打印出来
        System.out.println("****** HTTP request ******");
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(request.getMethod() + " ");
        strBuilder.append(uriInfo.getRequestUri().toString() + " ");
        strBuilder.append("HTTP/1.1[\\r\\n]");
        System.out.println(strBuilder.toString());
        MultivaluedMap<String, String> headers = httpHeader.getRequestHeaders();
        Iterator<String> iterator = headers.keySet().iterator();
        while(iterator.hasNext()){
            String headName = iterator.next();
            System.out.println(headName + ":" + headers.get(headName) + "[\\r\\n]");
        }
        System.out.println("[\\r\\n]");
        String responseStr =resourceName + "[" + description + "]";
        return responseStr;
    }
}
